package mao.leetcode.others;

public class Leetcode130 {

    public void solve(char[][] board) {
        int m = board.length;
        int n = board[0].length;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                boolean edge = i==0 || i==m-1 || j==0 ||j==n-1;
                if (edge && board[i][j] == 'O')
                    dfs(board,i,j);
            }
        }

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (board[i][j] == '#')
                    board[i][j] = 'O';
                else if (board[i][j] == 'O')
                    board[i][j] = 'X' ;
             }
        }

    }

    public void dfs(char[][] board,int i,int j){
        if (i<0 || i>board.length || j<0 || j>board[0].length || board[i][j] == '#' || board[i][j] == 'X')
            return;
        board[i][j] = '#';
        dfs(board,i-1,j);
        dfs(board,i+1,j);
        dfs(board,i,j-1);
        dfs(board,i,j+1);
    }

    public static void main(String[] args) {

    }

}
